{% extends 'layout/basic.html' %}
{% load static %}
{#title#}
{% block title %}用户短信登录{% endblock %}
{#css#}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
        }
    </style>
{% endblock %}

{#body#}
{% block content %}
    <div class="account">
        <div class="title">用户短信登录</div>
        <form id="regForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                </div>
                <div>
                    <div style="float: right">
                        <a href="{% url 'login' %}">用户名密码登录</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
{% endblock %}



{% block js %}

    <script>
        $(function () {
            bindClickBtnSms()
            bindClickBtnSubmit()
        });
        {#验证码倒计时#}
        function sendSmsRemind(){
            var $smsBtn=$('#btnSms');

            $smsBtn.prop('disabled',true);

            var time=60;
            var remind=setInterval(function(){
                $smsBtn.val(time+'秒重新发送')

                time=time-1

                if(time<1){
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled',false)
                }
            },1000)
        }

        {#点击获取验证码发送短信#}
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty();

                mobile_phone = $('#id_mobile_phone').val()

                $.ajax({
                    url: '/send/sms/',
                    type: 'GET',
                    data: {mobile_phone: mobile_phone,tpl:'login'},
                    dataType: 'JSON',
                    success: function (res) {
                        if(res.status){
                            console.log("发送成功");
                            sendSmsRemind();
                        }
                        else{
                            //返回错误信息
                            console.log(res)
                            $.each(res.errors,function(key,value){
                                $('#id_'+key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        {#点击登录#}
        function bindClickBtnSubmit(){
            $('#btnSubmit').click(function(){
                $('.error-msg').empty();

                $.ajax({
                    url:'/login/sms/',
                    type:'POST',
                    data:$('#regForm').serialize(),
                    dataType: 'JSON',
                    success:function (res){
                        if(res.status){
                            location.href=res.data
                        }else{
                            $.each(res.errors,function(key,value){
                                $('#id_'+key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}